package o;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import androidx.annotation.Nullable;
import com.huawei.appgallery.foundation.account.bean.UserSession;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.updatesdk.UpdateSdkAPI;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import java.lang.ref.WeakReference;
import o.dky.j;

public class eod {
    private static long ᐝ = 0;
    private boolean ʽ = false;
    private boolean ˊ = true;
    private Bundle ˋ;
    private boolean ˎ = false;
    private boolean ˏ = false;
    private ccw ॱ;
    private int ॱॱ = 0;

    class a extends eow {
        private final WeakReference<Context> ˎ;
        final /* synthetic */ eod ॱ;

        private a(eod o_eod, Context context) {
            this.ॱ = o_eod;
            this.ˎ = new WeakReference(context);
        }

        public void onLogin(CloudAccount[] cloudAccountArr, int i) {
            Logger.ˎ("AccountManagerHelper", "AccountLoginHandler sdk onLogin successed");
            ccm ˏ = eod.ˋ(cloudAccountArr, i);
            if (ˏ == null) {
                eok.ˎ().ॱ(12, 0, "onLogin 'accountInfo' is null.");
                Logger.ˎ("AccountManagerHelper", "AccountLoginHandler onLogin  accountInfo is null");
                if (this.ॱ.ॱॱ == 0) {
                    this.ॱ.ॱॱ = this.ॱ.ॱॱ + 1;
                    Context ˋ = ContentManager.getInstant().getContext();
                    this.ॱ.ˋ.putBoolean(CloudAccountManager.KEY_AIDL, true);
                    this.ॱ.ʽ(ˋ);
                    return;
                }
                eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10101));
                this.ॱ.ᐝ();
                this.ॱ.ॱ.ˋ(false, null);
                eos.ˎ("auto_login_type");
                return;
            }
            boolean z;
            Context ˋ2 = ContentManager.getInstant().getContext();
            String ॱ = eqp.ॱ();
            UserSession.getInstance().refreshAccount(ˏ);
            String ॱ2 = eqp.ॱ();
            if (TextUtils.isEmpty(ॱ2) || ॱ2.equals(ॱ)) {
                z = false;
            } else {
                if (!this.ॱ.ˏ) {
                    eqq.ˋ().ˋ(201);
                }
                z = true;
            }
            Logger.ˎ("AccountManagerHelper", "isFromStartUp=" + this.ॱ.ˏ + ", homeCountryChanged: " + z + " - " + ॱ2 + ", needGetUserInfo: " + this.ॱ.ˊ);
            if (!z) {
                if (this.ॱ.ˋ.getBoolean(CloudAccountManager.KEY_NEEDAUTH, false) && !TextUtils.isEmpty(ˏ.ᐝ())) {
                    eoy.ˎ(ˋ2, this.ॱ.ˏ);
                }
                eox.ˋ();
                if (this.ॱ.ˊ) {
                    eox.ˎ(ˋ2, this.ॱ.ˏ);
                }
            }
            this.ॱ.ॱ.ˋ(true, ˏ);
            eos.ˎ("auto_login_type");
        }

        public void onError(ErrorStatus errorStatus) {
            int errorCode = errorStatus.getErrorCode();
            Logger.ˋ("AccountManagerHelper", "AccountLoginHandler onError, ErrorCode:" + errorCode + ", ErrorReason: " + errorStatus.getErrorReason());
            eok.ˎ().ॱ(11, errorStatus.getErrorCode(), errorStatus.getErrorReason());
            super.onError(errorStatus);
            Context context;
            if (errorCode == 30) {
                context = (Context) this.ˎ.get();
                if (context == null) {
                    context = ContentManager.getInstant().getContext();
                }
                this.ॱ.ˋ.putBoolean(CloudAccountManager.KEY_NEEDAUTH, true);
                this.ॱ.ˋ.putBoolean(CloudAccountManager.KEY_AIDL, false);
                this.ॱ.ʽ(context);
            } else if (errorCode == 39 && eod.ᐝ == 0) {
                eod.ˎ();
                context = (Context) this.ˎ.get();
                if (context == null) {
                    context = ContentManager.getInstant().getContext();
                }
                Logger.ˋ("AccountManagerHelper", "The HwId login timed-out, try to start it.");
                eod.ॱˊ(context);
                SystemClock.sleep(800);
                this.ॱ.ʽ(context);
            } else {
                if (errorCode == 3002) {
                    eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10102));
                    if (this.ॱ.ॱ()) {
                        eua.ˏ().ˋ("st_error_retry_cancel", true);
                    }
                } else {
                    eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10101));
                    if (!this.ॱ.ˊ()) {
                        evk.ˏ(ContentManager.getInstant().getContext(), j.ॱॱ, 0).ˋ();
                    }
                }
                this.ॱ.ᐝ();
                this.ॱ.ॱ.ˋ(false, null);
                eos.ˎ("auto_login_type");
            }
        }

        protected String ˊ() {
            return "getAccountsByType";
        }

        public void onLogout(CloudAccount[] cloudAccountArr, int i) {
            Logger.ˎ("AccountManagerHelper", "AccountLoginHandler onLogout " + i);
        }

        public void onFinish(CloudAccount[] cloudAccountArr) {
        }
    }

    static class b implements eoi {
        private boolean ˋ;
        private boolean ˏ;

        b(boolean z, boolean z2) {
            this.ˋ = z;
            this.ˏ = z2;
        }

        public void ˎ(int i) {
            Context ˋ = ContentManager.getInstant().getContext();
            if (i != 1) {
                Logger.ˏ("AccountManagerHelper", "autoLogin, has no account so needn't login.");
                if (UserSession.getInstance().isLoginSuccessful()) {
                    eod.ˏ(ˋ);
                } else {
                    UserSession.getInstance().setStatus(0);
                }
            } else if (eua.ˏ().ˏ("st_error_retry_cancel", false)) {
                UserSession.getInstance().setStatus(0);
                Logger.ˏ("AccountManagerHelper", "user has cancel the login dialog.do not auto login again.");
            } else {
                Logger.ˎ("AccountManagerHelper", "start auto login");
                if (this.ˋ) {
                    eod.ˊ(ˋ, eoq.ˏ("auto_login_from_desktop"), false, this.ˏ, false);
                } else {
                    eod.ˊ(ˋ, eoq.ˏ("auto_login"), false, true, false);
                }
            }
        }
    }

    static /* synthetic */ long ˎ() {
        long j = ᐝ;
        ᐝ = 1 + j;
        return j;
    }

    private eod() {
    }

    public static boolean ˋ(Context context) {
        if (context == null) {
            return false;
        }
        return AccountLoginChecker.ˏ().ॱ(context);
    }

    public static void ˏ(Context context, eoi o_eoi, boolean z) {
        if (z) {
            AccountLoginChecker.ˏ().ˊ(context, new eog(o_eoi));
        } else {
            AccountLoginChecker.ˏ().ˊ(context, o_eoi);
        }
    }

    public static void ˊ(Context context, @Nullable ccw o_ccw, boolean z, boolean z2, boolean z3) {
        if (!ॱॱ(context)) {
            if (eqp.ᐝ()) {
                UpdateSdkAPI.setServiceZone("CN");
            } else {
                UpdateSdkAPI.setServiceZone("IE");
            }
            if (((eov) cdb.ॱ(eov.class)).ॱ(context)) {
                ((eov) cdb.ॱ(eov.class)).ˋ(context);
                return;
            }
            Logger.ˎ("AccountManagerHelper", "enter login");
            eod o_eod = new eod();
            o_eod.ˊ(z);
            o_eod.ˋ(z2);
            if (o_ccw == null) {
                o_eod.ॱ = eoq.ˊ();
            } else {
                o_eod.ॱ = o_ccw;
            }
            if (o_eod.ॱ != null) {
                ccr ˏ = o_eod.ॱ.ˏ();
                o_eod.ˏ(ˏ.ॱ());
                o_eod.ॱ(ˏ.ˏ());
                ॱ(context, o_eod);
            }
        }
    }

    private static void ॱ(final Context context, final eod o_eod) {
        CloudAccountManager.initial(context, new Bundle(), new ccs() {
            public void onFinish(Bundle bundle) {
                if (bundle != null) {
                    Logger.ˎ("AccountManagerHelper", "initial onFinish, hwid versionName=" + bundle.getString(CloudAccountManager.KEY_VERSION_NAME));
                }
                if (eod.ॱॱ(context)) {
                    eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10101));
                } else {
                    o_eod.ˏ(context, o_eod.ॱ.ˏ());
                }
            }

            public void onError(ErrorStatus errorStatus) {
                int i = -1;
                if (errorStatus != null) {
                    Logger.e("AccountManagerHelper", "initial onError, ErrorCode: " + errorStatus.getErrorCode() + ", ErrorReason: " + errorStatus.getErrorReason());
                    i = errorStatus.getErrorCode();
                    super.onError(errorStatus);
                } else {
                    Logger.e("AccountManagerHelper", "initial onError, but error is null.");
                }
                eok.ˎ().ॱ(10, i, "Failed to initialize CloudAccountManager.");
                if (i == 34) {
                    eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10104));
                } else if (i == 35) {
                    eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10105));
                } else {
                    eoj.ˋ().ˋ(new ccp(DialogButton.CONFIRM, 10103));
                }
                o_eod.ॱ.ˋ(false, null);
            }

            protected String ˋ() {
                return "initial";
            }
        });
    }

    public static void ॱ(Context context) {
        ˊ(context, null, false, true, false);
    }

    public static void ˏ(Context context) {
        try {
            UserSession.getInstance().setLastHomeCountry(eqp.ॱ());
            Logger.ˎ("AccountManagerHelper", "enter logoutOperation");
            if (context != null) {
                eoy.ˊ(context);
            }
            eoj.ˋ().ˋ(new ccp(103));
        } catch (Exception e) {
            Logger.ˋ("AccountManagerHelper", "logoutOperation Exception");
        }
    }

    public static void ˎ(Context context) {
        Logger.ˏ("AccountManagerHelper", "enter logout");
        if (!ॱॱ(context)) {
            Logger.ˏ("AccountManagerHelper", "enter logout logoutHwIDByUserID");
            CloudAccountManager.logoutHwIdByUserId(context, UserSession.getInstance().getUserId(), new e(), new Bundle());
        }
    }

    public static void ˋ() {
        Logger.ˎ("AccountManagerHelper", "Reset autoLogin retry times.");
        ᐝ = 0;
    }

    public static void ˋ(Context context, boolean z, boolean z2) {
        try {
            if (!diu.ˊ().ˎ()) {
                Logger.ˎ("AccountManagerHelper", "Sorry, hasAgreedPotocal is false , Can not auto login.");
            } else if (!doo.ˊ(context)) {
                Logger.ˎ("AccountManagerHelper", "Sorry, network is bad. Can not auto login.");
            } else if (((eov) cdb.ॱ(eov.class)).ॱ(context)) {
                Logger.ˎ("AccountManagerHelper", "Sorry, need interrupt autologin. Can not auto login.");
            } else {
                int status = UserSession.getInstance().getStatus();
                if (status == 0 || status == 1) {
                    UserSession.getInstance().setStatus(2);
                    ˏ(context, new b(z, z2), false);
                }
            }
        } catch (Exception e) {
            Logger.e("AccountManagerHelper", "autoLogin Exception");
        }
    }

    public static void ˊ(Context context) {
        ˋ(context, false, true);
    }

    public static void ᐝ(Context context) {
        Logger.ˏ("AccountManagerHelper", "st has expried for wap , will loginWhenStExpired....");
        eod o_eod = new eod();
        o_eod.ॱ(true);
        o_eod.ॱ = eoq.ˊ();
        ccr ˏ = o_eod.ॱ.ˏ();
        ˏ.ˋ(true);
        ˏ.ॱ(false);
        o_eod.ˏ(context, ˏ);
    }

    private static ccm ˋ(CloudAccount[] cloudAccountArr, int i) {
        if (cloudAccountArr == null || i < 0 || cloudAccountArr.length <= i) {
            Logger.e("AccountManagerHelper", "getCorrectAccount, mAccounts is null or index is invalid.");
            return null;
        }
        Logger.ˏ("AccountManagerHelper", "getCorrectAccount, mAccounts.length = " + cloudAccountArr.length + ", index = " + i);
        CloudAccount cloudAccount = cloudAccountArr[i];
        if (cloudAccount != null) {
            Bundle accountInfo = cloudAccount.getAccountInfo();
            if (accountInfo == null) {
                Logger.e("AccountManagerHelper", "getCorrectAccount, CloudAccount.getAccountInfo() is null.");
                return null;
            }
            String string = accountInfo.getString("userId");
            String authToken = cloudAccount.getAuthToken();
            if (dof.ˋ(string) || dof.ˋ(authToken)) {
                Logger.e("AccountManagerHelper", "getCorrectAccount, mAccounts is invalid because userId or serviceToken is null.");
                return null;
            }
            ccm o_ccm = new ccm();
            o_ccm.ˋ(string);
            o_ccm.ˎ(authToken);
            o_ccm.ˊ(accountInfo.getString("loginUserName"));
            o_ccm.ॱ(accountInfo.getString("accountName"));
            o_ccm.ˏ(accountInfo.getString("deviceType"));
            o_ccm.ॱॱ(accountInfo.getString("deviceId"));
            o_ccm.ʽ(accountInfo.getString("serviceCountryCode"));
            return o_ccm;
        }
        Logger.e("AccountManagerHelper", "getCorrectAccount, mAccounts[index] is null.");
        return null;
    }

    private static boolean ॱॱ(Context context) {
        if (doo.ˊ(context)) {
            return false;
        }
        evk.ˋ(context, context.getResources().getString(j.ॱʼ), 0).ˋ();
        return true;
    }

    private boolean ॱ() {
        return this.ʽ;
    }

    private void ॱ(boolean z) {
        this.ʽ = z;
    }

    boolean ˊ() {
        return this.ˎ;
    }

    void ˏ(boolean z) {
        this.ˎ = z;
    }

    private void ʽ(Context context) {
        UserSession.getInstance().setStatus(3);
        eos.ˎ(context, this.ˋ, new a(context), "auto_login_type");
    }

    private void ˏ(Context context, ccr o_ccr) {
        Logger.ˎ("AccountManagerHelper", "enter doLogin needAuth=" + o_ccr.ˋ() + ",will getAccountsByType");
        this.ˋ = o_ccr.ˊ();
        ʽ(context);
    }

    private void ᐝ() {
        Context ˋ = ContentManager.getInstant().getContext();
        if (UserSession.getInstance().isLoginSuccessful()) {
            ˏ(ˋ);
        } else {
            UserSession.getInstance().setStatus(0);
        }
    }

    private void ˊ(boolean z) {
        this.ˏ = z;
    }

    private void ˋ(boolean z) {
        this.ˊ = z;
    }

    private static boolean ॱˊ(Context context) {
        Intent intent = new Intent();
        intent.setClassName("com.huawei.hwid", HuaweiApiAvailability.ACTIVITY_NAME);
        intent.putExtra("intent.extra.hms.core.DELEGATE_NAME", "Startup.Jump");
        if (context.getPackageManager().resolveActivity(intent, 0) == null) {
            Logger.ˋ("AccountManagerHelper", "Not exist HwID");
        } else {
            try {
                if (!(context instanceof Activity)) {
                    intent.setFlags(268435456);
                }
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Logger.ˋ("AccountManagerHelper", "Not exist HwID");
            } catch (Throwable e2) {
                Logger.ˋ("AccountManagerHelper", "Exception when calling startActivity.", e2);
            }
        }
        return false;
    }
}
